package com.bjpowernode.learndrools.config;

import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.runtime.KieContainer;
import org.kie.internal.io.ResourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * drools配置类
 */
@Configuration
public class DroolsConfiguration {
    /**
     * 获取drools的服务实例
     */
    private static final KieServices kieServices = KieServices.Factory.get();

    /**
     * 规则文件路径
     */
    private static final String RULES_PATH = "rules/user.drl";

    @Bean
    public KieContainer getKieContainer() {
        //文件系统对象
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        //传入规则文件路径
        kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH));

        //获取builder对象
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);

        //构建
        kieBuilder.buildAll();

        KieModule kieModule = kieBuilder.getKieModule();

        KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());

        return kieContainer;

    }
}
